/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.component.cim.pusher;

import com.workcell.workdata.im.component.event.GroupEvent;
import com.workcell.workdata.im.constant.MessageAction;
import com.workcell.workdata.im.entity.EventMessage;
import com.workcell.workdata.im.entity.Group;
import com.workcell.workdata.im.entity.Message;
import com.workcell.workdata.im.exception.ChatBlockedException;
import com.workcell.workdata.im.model.app.Origin;
import com.workcell.workdata.im.model.dto.MessageTitle;
import com.workcell.workdata.im.model.vo.GroupVO;
import com.workcell.workdata.im.service.GroupMemberService;
import com.workcell.workdata.im.service.GroupService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 推送群消息
 */
@Component
public class GroupMessagePusher extends BaseMessagePusher {

	@Resource
	private GroupService groupService;

	@Resource
	private GroupMemberService groupMemberService;


	public boolean isGroupBlocked(Long id,Long uid){

		GroupVO group = groupService.findOne(id,false);

		if (group == null){
			return true;
		}

		boolean isFounder = Objects.equals(uid,group.getUid());

		return group.getState() == Group.STATE_BLOCKED && !isFounder;

	}

 	@Override
	public void preHandle(Message message, Origin origin) {


 		Long uid = message.getSender();

 		Long groupId = message.getReceiver();

 		if (isGroupBlocked(groupId,uid)){
 			throw new ChatBlockedException();
		}

		/*
		 * 推送的消息体中增加消息发送人name信息
 		 */
		String name = groupMemberService.findName(groupId,uid);

		MessageTitle title = MessageTitle.of(message.getTitle());
		title.setName(name);
		message.setTitle(title.toString());

	}

	@Override
	public void push(final Message message,Origin origin) {

		Long sender = message.getSender();

		Long groupId = message.getReceiver();

		message.setSender(groupId);

		message.setExtra(sender.toString());

		List<Long> idList = groupMemberService.findUidList(groupId);
		idList.remove(sender);

		super.push(message,idList);

		if (!Objects.equals(message.getAction(), MessageAction.ACTION_3)){
			return;
		}

		/*
		 * 给自己其他在线设备同步一条发送的群聊消息
		 * 排除当前设备 origin.getAppChannel()
		 */
		Message syncMessage = new EventMessage();
		syncMessage.setId(message.getId());
		syncMessage.setSender(groupId);
		syncMessage.setReceiver(sender);
		syncMessage.setAction(MessageAction.ACTION_320);
		syncMessage.setFormat(message.getFormat());
		syncMessage.setContent(message.getContent());
		syncMessage.setTitle(message.getTitle());
		syncMessage.setCreateTime(message.getCreateTime());

		super.push(syncMessage,origin.getAppChannel());

	}


	@EventListener
	@Order(0)
	public void onGroupEvent(GroupEvent event){

		Message message = event.getSource();

		long groupId = message.getSender();

		if (CollectionUtils.isEmpty(event.getUidList())){
			event.setUidList(groupMemberService.findUidList(groupId));
		}

		if (event.getUid() != null){
			MessageTitle title = MessageTitle.of(message.getTitle());
			title.setName(groupMemberService.findName(groupId,event.getUid()));
			message.setTitle(title.toString());
		}

		super.push(message,event.getUidList());
	}

}
